﻿namespace TheCollectiveNetwork.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.Objects;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.Threading;
    using TCNDataAccessLayer;
    using TheCollectiveNetwork.Classes.GlobalFunctions;
    using TheCollectiveNetwork.Classes.Security;

    /// <summary>
    ///   RIA Domain Service that exposes methods for performing user registrations.
    /// </summary>
    /// 

    [CLSCompliant(false)]   // base type exposes _AppDomain which is not cls-compliant as it begins with an underscore so turn cls compliance off for this class
    [EnableClientAccess]
    public class UserRegistrationService : LinqToEntitiesDomainService<TheCollectiveNetworkEntities>
    {
        // Dummy rountine only. Required as InsertDTOAccountsRegisterUser will return an error if there
        // is not an associated get routine.        
        public IQueryable<DTOAccountsRegisterUser> GetAccounts_Registration_Requests()
        {     
            return null;
        }

        // Insert routine for Accounts_User_details
        public void InsertDTOAccountsRegisterUser(DTOAccountsRegisterUser dtoaru)
        {
            ObjectParameter returncode;
            ObjectParameter returnmessage;
            DTOreturnmessage dtomessage;
            DomainException domainException;
            String ValidationErrorMessage;
            bool ValidationError;
            
            //Initialise variables
            ValidationErrorMessage = String.Empty;
            ValidationError = false;

            // Sleep - for security - to slow down multiple registration attempts
            Thread.Sleep(500);

            // Validate the input fields
            if (dtoaru.first_name.Length < 3 || dtoaru.last_name.Length < 3 || dtoaru.organisation.Length < 3 || dtoaru.phone_number.Length <3)
            {
                ValidationErrorMessage = ValidationErrorMessage + @"Error - all fields must contain more than 3 characters";
                ValidationError = true;
            }

            // Validate the password
            if (dtoaru.passwordencrypt.Length < 6)
            {
                //Append a comma if there is already text in the error message
                if (ValidationErrorMessage.Length > 0)
                {
                    ValidationErrorMessage = ValidationErrorMessage + @", Password must contain at least 6 characters";
                }
                else 
                {
                    ValidationErrorMessage = ValidationErrorMessage + @"Error - Password must contain at least 6 characters";
                }
                                               
                ValidationError = true;
            }

            // Attempt the to insert the registration if all the fields validate
            if (ValidationError == false)
            {
                //encrypt the password
                dtoaru.passwordencrypt = TCNMembershipProvider.sha512encrypt(dtoaru.passwordencrypt);

                // Create output parameters
                returncode = new ObjectParameter("returncode", typeof(int));
                returnmessage = new ObjectParameter("returnmessage", typeof(string));

                // Perform the insert
                this.ObjectContext.accounts_register_user(dtoaru.first_name,
                                                          dtoaru.last_name,
                                                          dtoaru.phone_number,
                                                          dtoaru.organisation,
                                                          dtoaru.email,
                                                          dtoaru.passwordencrypt,
                                                          returncode,
                                                          returnmessage);

                // Get the return code and message
                dtomessage = new DTOreturnmessage();
                dtomessage.returncode = (int)returncode.Value;
                dtomessage.returnmessage = (string)returnmessage.Value;

                // Throw an error if the returncode is not zero
                if (dtomessage.returncode > 0)
                {
                    domainException = new DomainException(dtomessage.returnmessage, dtomessage.returncode);
                    throw domainException;
                }

            }
            else
            {
                // Throw a domain exeception if the input does not validate and return this to the user
                domainException = new DomainException(ValidationErrorMessage, 1);
                throw domainException;
            }

        }

        
     }

}